using System;
using System.Collections.Generic;

namespace GymManager.Mechanism
{
    /// <summary>
    /// This class is responsible by emulate an HTTP Session
    /// to windows forms. Using this, is possible to store data 
    /// like a http session and create divisions at main session.  
    /// </summary>
    public class Session
    {
        // Dictionary where the data regarding session will be stored
        private IDictionary<Object, Object> _sessionDictionary = null;

        // Singleton instance of Session
        private static Session _singletonInstance = null;

        /// <summary>
        /// Private constructor which initializes internal structures
        /// </summary>
        private Session()
        {
            // initializing session dictionary
            _sessionDictionary = new Dictionary<Object, Object>();
        }
                
        /// <summary>
        /// Method responsible by getting a reference to Session
        /// </summary>
        /// <returns>Session - Session singleton instance</returns>
        public static Session getInstance()
        {
            if (_singletonInstance == null)
            {
                _singletonInstance = new Session();
            }

            return _singletonInstance;
        }

        /// <summary>
        /// This method is responsible by putting information into session.
        /// </summary>
        /// <param name="Object">key under value will be stored</param>
        /// <param name="Object">values associated to chosen key</param>
        public void put(Object key, Object value)
        {
            if (_sessionDictionary.ContainsKey(key))
            {
                _sessionDictionary.Remove(key);
            }
            
            _sessionDictionary.Add(key, value);
        }

        /// <summary>
        /// This method is responsible by removing information from session.
        /// </summary>
        /// <param name="Object">key used to remove the value from session</param>
        public void remove(Object key)
        {
            _sessionDictionary.Remove(key);
        }

        /// <summary>
        /// This method is responsible by getting information from session.
        /// </summary>
        /// <param name="Object">key used to retrieve the value from session</param>
        /// <returns>Object - value returned which was associated to a key</returns>
        public Object get(Object key)
        {
            if (_sessionDictionary.ContainsKey(key))
            {
                return _sessionDictionary[key];
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// This method is responsible by invalidating this session
        /// </summary>
        public void invalidateSession()
        {
            _sessionDictionary.Clear();
            _singletonInstance = null;
        }

        /// <summary>
        /// This method produces a dump from this session
        /// </summary>
        /// <returns> String representation from session </returns>
        public String dumpSession()
        {
            return _sessionDictionary.ToString();
        }
    }
}
